package com.frame.management;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Map;
import java.util.Vector;

import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumnModel;

import com.excel.Utils;
import com.excel.student.Student;

public class MainSystemTemp {
	private JTable table = null;
	private StudentModel model = null;
	private JScrollPane s_pan = null;

	public int line = 2;
	private JFrame frame = new JFrame("*****欢迎登录宿舍安全卫生检查系统*****");
	private JPanel panel = new JPanel();

	public void caidan() {
		JMenu menu = new JMenu("操作");

		JMenuBar menuBar = new JMenuBar();
		JMenuItem insert = new JMenuItem("增加");
		JMenuItem delete = new JMenuItem("删除");
		JMenuItem update = new JMenuItem("修改");
		JMenuItem search = new JMenuItem("查询");

		menu.add(insert);
		menu.add(delete);
		menu.add(update);
		menu.add(search);

		menuBar.add(menu);

		frame.setJMenuBar(menuBar); // 在窗体中加入JMenuBar组件

		// 添加
		insert.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				MainView it = new MainView();
				it.daoru();
				frame.dispose();
			}
		});
		
		//删除数据
		delete.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				model.removeRow(table.getSelectedRow());
			}
		});
		
		

		model = new StudentModel();
		table = new JTable(model);
		table.setBackground(Color.white);
		TableColumnModel tcm = table.getColumnModel();
		//final JCheckBox checkBox = new JCheckBox();
		//tcm.getColumn(0).setCellEditor(new DefaultCellEditor(checkBox));
		tcm.getColumn(0).setCellRenderer(new TableCellRenderer() {
			
			@Override
			public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
					boolean hasFocus, int row, int column) {
				JCheckBox ck = new JCheckBox();
				// 使具有焦点的行对应的复选框选中
				ck.setSelected(isSelected);
				// 设置单选box.setSelected(hasFocus);
				// 使复选框在单元格内居中显示
				ck.setHorizontalAlignment((int) 0.5f);
				return ck;
			}
		});
		tcm.getColumn(0).setPreferredWidth(50);
		tcm.getColumn(1).setPreferredWidth(100);
		tcm.getColumn(2).setPreferredWidth(50);
		tcm.getColumn(3).setPreferredWidth(200);
		

		s_pan = new JScrollPane(table);

		frame.getContentPane().add(s_pan, BorderLayout.CENTER);
		frame.getContentPane().add(panel, BorderLayout.NORTH);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(500, 400);
		frame.setLocation(400, 170);
		frame.setVisible(true);
		
		table.setEnabled(true);
		table.setCellSelectionEnabled(true);
		
		
		
		//关闭窗口的事件，保存学生信息
		frame.addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				Utils.createStudentExcel();
				System.out.println("关闭窗口，学生信息保存到Excel。");
				super.windowClosing(e);
			}
		});
	}

}

@SuppressWarnings("rawtypes")
class StudentModel extends DefaultTableModel {

	private static final long serialVersionUID = -7495940408592595397L;

	private static Map<String, Student> map = Utils.studentMap;

	private static String[] title_name = { "选择", "学号", "姓名", "班级" };

	private static Vector<Vector> content = null;

	@SuppressWarnings("unchecked")
	public StudentModel() {
		content = new Vector<>(map.keySet().size());
		for (String key : map.keySet()) {
			Student s = map.get(key);
			Vector v = new Vector<>();
			v.add(0, false);
			v.add(1, s.getId());
			v.add(2, s.getName());
			v.add(3, s.getCla());
			content.add(v);
		}
	}

	public String getColumnName(int col) { // 返回列名，即表头
		return title_name[col];
	}

	@Override
	public int getRowCount() {
		return map.keySet().size();
	}

	@Override
	public int getColumnCount() {
		return title_name.length;
	}

	@Override
	public Object getValueAt(int rowIndex, int columnIndex) {
		return ((Vector) content.get(rowIndex)).get(columnIndex);
	}

	public Class<?> getColumnClass(int c) {
		if (c == 0) {
			return Boolean.class;
		}
		return super.getColumnClass(c);
	}

	@Override
	public void insertRow(int row, Vector rowData) {
		super.insertRow(row, rowData);
	}


	@Override
	public void setValueAt(Object aValue, int row, int column) {
		super.setValueAt(aValue, row, column);
	}

	
	public void removeSelectedRow(){
		for (int i = 0; i < content.size(); i++) {
			Vector v = content.get(i);
			if((boolean) v.get(0)){
				removeRow(i);
			}
		}
	}
	
	
}
